<!-- 编辑任务：新增压测脚本 -->
<template>
  <el-dialog title="任务编辑" :visible.sync="visible" width="55%" :before-close="handleClose">
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
      <el-tab-pane label="新增脚本" name="first">
        <el-table :data="scripts" style="width: 100%">
          <el-table-column label="脚本">
            <template slot-scope="scope">
              <el-select v-model="scope.row.scriptId" filterable placeholder="请选择" @change="debugSelect">
                <el-option v-for="resource in scriptList" :key="resource.id" :label="resource.name" :value="resource.id" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="预设并发">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.users" :min="1" :max="30000" controls-position="right" label="执行时限定的并发值" />
            </template>
          </el-table-column>
          <el-table-column label="预设QPS（0表示不限制）">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.qps" :min="0" :max="1000000" controls-position="right" label="执行时限定的最大QPS" />
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleAdd(scope.$index, scope.row)">增加</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="taskAddScriptsHandle">保存</el-button>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
import { taskAddScripts } from '@/api/perf/task.js'
import { searchAll } from '@/api/perf/script.js'
export default {
  data() {
    return {
      visible: false,
      taskId: '',
      scriptList: [],
      activeName: 'first',
      scripts: [{ users: 100, qps: 0 }]
    }
  },
  methods: {
    debugTest() {
      console.info(this.scripts)
    },
    init(taskId) {
      this.visible = true
      this.taskId = taskId
      searchAll(1, 100000).then(respnose => {
        if (respnose && respnose.code === 200) {
          const { data } = respnose
          this.scriptList = data.content
        } else {
          this.$message.warning(respnose.message)
        }
      })
    },
    debugSelect(value) {
      // console.info(value)
    },
    handleAdd(index, row) {
      this.scripts.push({ users: 100, qps: 0 })
    },
    handleDelete(index, row) {
      if (this.scripts.length === 1) {
        this.$message.warning('最后一条记录不可删除')
        return
      }
      this.scripts.splice(index, 1)
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },
    taskAddScriptsHandle() {
      const data = []
      for (var i = 0; i < this.scripts.length; i++) {
        data.push({
          'taskId': this.taskId,
          'scriptId': this.scripts[i].scriptId,
          'qps': this.scripts[i].qps,
          'users': this.scripts[i].users
        })
      }
      taskAddScripts(data).then(respnose => {
        if (respnose && respnose.code === 200) {
          this.$message.success(respnose.message)
          this.$emit('reflushDataList')
        } else {
          this.$message.error(respnose.message)
        }
      })
    },
    handleClose(done) {
      done()
    }
  }
}
</script>

<style>
</style>
